package com.zr.task.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import com.zr.task.domain.EimTaskLog;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.zr.common.annotation.Log;
import com.zr.common.core.controller.BaseController;
import com.zr.common.core.domain.AjaxResult;
import com.zr.common.enums.BusinessType;
import com.zr.task.domain.EimTaskDetect;
import com.zr.task.service.IEimTaskDetectService;
import com.zr.common.utils.poi.ExcelUtil;
import com.zr.common.core.page.TableDataInfo;

/**
 * 检测任务Controller
 * 
 * @author zhaojunqi
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/eim/detect")
public class EimTaskDetectController extends BaseController
{
    @Autowired
    private IEimTaskDetectService eimTaskDetectService;

    /**
     * 体温核酸检测任务
     * @param eimTaskDetect 检测任务详情
     */
    @GetMapping("/submit")
    @Transactional
    public void submit(EimTaskDetect eimTaskDetect, @RequestParam("taskType") String taskType, @RequestParam("personId") String personId)
    {
        //更新子单
        eimTaskDetectService.updateEimTaskDetect(eimTaskDetect);
        //更新父单
        eimTaskDetectService.updateEimTaskCommandSchedule(eimTaskDetect.getPid());
        //更新日志
        //增加完成日志
        EimTaskLog eimTaskLog = new EimTaskLog();
        eimTaskLog.setTaskId(Long.parseLong(eimTaskDetect.getPid()));
        eimTaskLog.setTaskType(taskType);
        eimTaskLog.setTaskStatus("3");
        eimTaskLog.setTaskUserId(personId);
        eimTaskLog.setCreateTime(new Date());
        eimTaskDetectService.addTaskTranshipmentLog(eimTaskLog);
    }

    /**
     * 查询核酸检测详情列表
     * @param eimTaskDetect 检测任务详情
     * @return
     */
    @GetMapping("/list")
    @PermitAll
    public TableDataInfo list(EimTaskDetect eimTaskDetect)
    {
        startPage();
        List<EimTaskDetect> list = eimTaskDetectService.selectEimTaskDetectList(eimTaskDetect);
        return getDataTable(list);
    }

    /**
     * 导出检测任务列表
     */
    @PreAuthorize("@ss.hasPermi('eim:detect:export')")
    @Log(title = "检测任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EimTaskDetect eimTaskDetect)
    {
        List<EimTaskDetect> list = eimTaskDetectService.selectEimTaskDetectList(eimTaskDetect);
        ExcelUtil<EimTaskDetect> util = new ExcelUtil<EimTaskDetect>(EimTaskDetect.class);
        util.exportExcel(response, list, "检测任务数据");
    }

    /**
     * 获取检测任务详细信息
     */
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestParam("id") Long id)
    {
        return AjaxResult.success(eimTaskDetectService.selectEimTaskDetectById(id));
    }

    /**
     * 新增检测任务
     */
    @PreAuthorize("@ss.hasPermi('eim:detect:add')")
    @Log(title = "检测任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EimTaskDetect eimTaskDetect)
    {
        return toAjax(eimTaskDetectService.insertEimTaskDetect(eimTaskDetect));
    }

    /**
     * 修改检测任务
     */
    @PreAuthorize("@ss.hasPermi('eim:detect:edit')")
    @Log(title = "检测任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EimTaskDetect eimTaskDetect)
    {
        return toAjax(eimTaskDetectService.updateEimTaskDetect(eimTaskDetect));
    }

    /**
     * 删除检测任务
     */
    @PreAuthorize("@ss.hasPermi('eim:detect:remove')")
    @Log(title = "检测任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eimTaskDetectService.deleteEimTaskDetectByIds(ids));
    }

    /**
     * 创建子任务
     * 需求：APP
     */
    @PostMapping("/addByLeader")
    public AjaxResult addByLeader(EimTaskDetect eimTaskDetect)
    {
        eimTaskDetectService.insertEimTaskDetect(eimTaskDetect);
        return AjaxResult.success(eimTaskDetect);
    }
}
